﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace nhahangbarota
{
    public partial class BanAn : Form
    {
        DataTable dt = new DataTable();
        public BanAn()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            Formchinh a = new Formchinh();
            a.ShowDialog();
        }

        private void BanAn_Load(object sender, EventArgs e)
        {
            BanAnBUS baBUS = new BanAnBUS();
            dt = baBUS.LoadDS();

            gvDSBan.Rows.Clear();
            int i = 1;
            foreach (DataRow dtRow in dt.Rows)
            {
                gvDSBan.Rows.Add(i, dtRow[0].ToString(), dtRow[1].ToString(), dtRow[2].ToString());
                i++;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int co = 0;
            for (int i = 0; i < listBan.Count; i++)
            {
                BanAnBUS baBUS = new BanAnBUS();
                if (baBUS.CapNhat(listBan[i]) == false)
                    co = 1;
            }

            if (co == 0)
                MessageBox.Show("Cập nhật thành công", "Thông báo");
            BanAn_Load(sender, e);
        }

        List<BanAnDTO> listBan = new List<BanAnDTO>();
        private void gvDSBan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = gvDSBan.CurrentRow.Index;

            BanAnDTO baDTO = new BanAnDTO();
            baDTO.MaBan = int.Parse(gvDSBan.Rows[index].Cells[1].Value.ToString());
            baDTO.SucChua = int.Parse(gvDSBan.Rows[index].Cells[3].Value.ToString());
            baDTO.TenBan = gvDSBan.Rows[index].Cells[2].Value.ToString();

            listBan.Add(baDTO);
        }

        private void btnThemBan_Click(object sender, EventArgs e)
        {
            ThemBan f1 = new ThemBan();
            f1.ShowDialog();
            BanAn_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int index = gvDSBan.CurrentRow.Index;

            int maBan = int.Parse(gvDSBan.Rows[index].Cells[1].Value.ToString());

            BanAnBUS baBUS = new BanAnBUS();
            if (baBUS.XoaBan(maBan) == true)
            {
                MessageBox.Show("Xóa thành công !!");
                BanAn_Load(sender, e);
            }
        }
    }
}
